package CURD.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.malty.MyBatisApplication;
import com.malty.entity.Employee;
import com.malty.service.EmployeeService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest(classes = MyBatisApplication.class)
@RunWith(SpringRunner.class)
public class ListQueryService {
    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    EmployeeService employeeService;

    @Test
    public void list() {
        // 查询所有记录
        List<Employee> list = employeeService.list();

        System.out.println(employeeService.count());
        System.out.println(list.size());
        System.out.println(list);
    }

    @Test
    public void listWrapper() {
        // 根据Wrapper条件查询
        List<Employee> list = employeeService.list(Wrappers.<Employee>lambdaQuery().eq(Employee::getAge, 22));
        System.out.println(list);
    }

    @Test
    public void listByIds() {
        // 根据 id 列表查询
        List<Long> list = new ArrayList<>();
        List<Employee> employeeList = employeeService.listByIds(list);
        System.out.println(employeeList);
    }

    @Test
    public void listByMap() {
        // 根据 列名和列的值 作为条件筛选查询
        HashMap<String, Object> map = new HashMap<>();
        map.put("age", 27);
        map.put("gender", "男");
        List<Employee> employeeList = employeeService.listByMap(map);
        System.out.println(employeeList);
    }

    @Test
    public void listMaps() {
        // 查询出所有记录，返回 字段 和 值的键值对
        List<Map<String, Object>> maps = employeeService.listMaps();
        for (Map<String, Object> map : maps) {
            System.out.println("key   :" + map.keySet());
            System.out.println("value :" + map.values());
        }
    }

    @Test
    public void listMapsWrapper() {
        // 根据Wrapper条件查询，返回 字段 和 值的键值对
        List<Map<String, Object>> maps = employeeService.listMaps(Wrappers.<Employee>lambdaQuery().eq(Employee::getGender, "男"));
        for (Map<String, Object> map : maps) {
            System.out.println("key   :" + map.keySet());
            System.out.println("value :" + map.values());
        }
    }

    @Test
    public void listObjs() {
        // 返回所有记录的第一个字段
        List<Object> objects = employeeService.listObjs();
        System.out.println(objects.toString());
    }

    @Test
    public void listObjsWrapper() {
        // 根据Wrapper筛选查询，返回对象封装
        // mapper 转换函数
        List<Object> list = employeeService.listObjs(Wrappers.<Employee>lambdaQuery().select(Employee::getLastName).eq(Employee::getGender, "男"));
        System.out.println(list.toString());
    }

    @Test
    public void listObjsMapper() {
        // 根据Mapper筛选查询，返回对象封装
        List<Integer> integers = employeeService.listObjs(s -> (Integer) s);
        System.out.println(integers.toString());
    }

    @Test
    public void listObjsWrapperMapper() {
        // 根据Wrapper 和 Mapper筛选查询，返回对象封装
        List<Integer> list = employeeService.listObjs(Wrappers.<Employee>lambdaQuery().eq(Employee::getGender, "女"), s -> (Integer) s);
        System.out.println(list.toString());
    }

}
